﻿using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;
using Bussines;
using System.Data.SqlClient;
using CustomControls.Grid;


namespace ownx
{
    public partial class wfNyugdij : System.Web.UI.Page
    {
        clNYUGDIJAK _clNYUGDIJAK;
        clTIPUS_LIST _clTipusok;
        clINTEZMENYEK_LIST _clIntezmenyek;
        DataSet dsNYUGDIJAK;

        int _PRT_ID = -1;

        #region Page load, datainit
        protected void Page_Load(object sender, EventArgs e)
        {
            TBLObj blObj = null;
            if (Session["BL"] != null)
            {
                blObj = (TBLObj)Session["BL"];
            }
            //TBLObj blObj = new TBLObj("Test", Session["CONSTR"].ToString(), (clFIELDINFO_LIST)Session["FIELDINFO"]);
            _PRT_ID = Convert.ToInt32(Session["PRT_ID"].ToString());
            _clNYUGDIJAK = new clNYUGDIJAK(blObj, _PRT_ID);
            _clTipusok = new clTIPUS_LIST(blObj, "NYG");
            _clIntezmenyek = new clINTEZMENYEK_LIST(blObj,3);

            dsNYUGDIJAK = _clNYUGDIJAK.GetNYUGDIJAK();

            //ha ez az első meghívás
            if (!(Page.IsPostBack))
            {
                DataInit();
            }
        }

        private void DataInit()
        {
            //feltöltöm a detasetet, majd a gridet
            dsNYUGDIJAK = _clNYUGDIJAK.GetNYUGDIJAK();

            gvNYUGDIJAK.DataSource = dsNYUGDIJAK;
            gvNYUGDIJAK.DataBind();

            FillCombos();

            if (dsNYUGDIJAK.Tables["NYUGDIJAK"].Rows.Count > 0)
            {
                //kijelölöm az elsőt, és kitöltöm az editeket
                gvNYUGDIJAK.SelectedIndex = 0;
                gvNYUGDIJAK_SelectedIndexChanged(gvNYUGDIJAK, null);
            }
        }
        private void FillCombos()
        {

            
            foreach (clTIPUS akt in _clTipusok.lTIPUS)
            {
                ListItem li = new ListItem(akt.fTPS_MEGNEVEZES, akt.fTPS_TIPUSKOD.ToString());
                ddlNYG_TIPUS.Items.Add(li);
            }

            foreach (clINTEZMENYEK akti in _clIntezmenyek.lINTEZMENYEK)
            {
                ListItem lii = new ListItem(akti.fINT_MEGNEVEZES , akti.fINT_ID.ToString());
                ddlNYG_INT_ID.Items.Add(lii);
            }
        }
        #endregion

        protected void gvNYUGDIJAK_SelectedIndexChanged(object sender, EventArgs e)
        {
            //lépegetés a gridben
            tbNYG_ID.Text = dsNYUGDIJAK.Tables["NYUGDIJAK"].Rows[gvNYUGDIJAK.SelectedIndex]["NYG_ID"].ToString();
            tbNYG_PRT_ID.Text = dsNYUGDIJAK.Tables["NYUGDIJAK"].Rows[gvNYUGDIJAK.SelectedIndex]["NYG_PRT_ID"].ToString();
            tbNYG_HOZAM.Text = Convert.ToDouble(dsNYUGDIJAK.Tables["NYUGDIJAK"].Rows[gvNYUGDIJAK.SelectedIndex]["NYG_HOZAM"].ToString()).ToString("F2"); 
            tbNYG_MEGJEGYZES.Text = dsNYUGDIJAK.Tables["NYUGDIJAK"].Rows[gvNYUGDIJAK.SelectedIndex]["NYG_MEGJEGYZES"].ToString();
            ddlNYG_TIPUS.SelectedValue = dsNYUGDIJAK.Tables["NYUGDIJAK"].Rows[gvNYUGDIJAK.SelectedIndex]["NYG_TIPUS"].ToString();
            ddlNYG_INT_ID.SelectedValue = dsNYUGDIJAK.Tables["NYUGDIJAK"].Rows[gvNYUGDIJAK.SelectedIndex]["NYG_INT_ID"].ToString(); 

            lMessage.Text = "";
        }

        protected void bUj_Click(object sender, EventArgs e)
        {
            dsNYUGDIJAK.Tables["NYUGDIJAK"].Rows.Add();

            gvNYUGDIJAK.DataSource = dsNYUGDIJAK;
            gvNYUGDIJAK.DataBind();

            gvNYUGDIJAK.SelectedIndex = dsNYUGDIJAK.Tables["NYUGDIJAK"].Rows.Count - 1;

            tbNYG_ID.Text = "-1";
            tbNYG_PRT_ID.Text = _PRT_ID.ToString();
            tbNYG_MEGJEGYZES.Text = "";
            tbNYG_HOZAM.Text = "0";
            ddlNYG_TIPUS.SelectedIndex = 0;
            ddlNYG_INT_ID.SelectedIndex = 0;

        }
        protected void bSave_Click(object sender, EventArgs e)
        {
            bool b = bSave_Click2(sender, e);
        }
        protected bool bSave_Click2(object sender, EventArgs e)
        {

            bool KellMent = true;
            int iNYG_ID; 
            int iNYG_TIPUS; 
            int iNYG_PRT_ID; 
            int iNYG_INT_ID; 
            double iNYG_HOZAM=0; 
            string iNYG_MEGJEGYZES;
            DateTime iNYG_CREATED;
            int iNYG_CREATED_BY;
            DateTime iNYG_MODIFIED;
            int iNYG_MODIFIED_BY;
            string oErr = "";
            try
            {


                iNYG_ID = Convert.ToInt32(tbNYG_ID.Text);
            }
            catch (Exception xx)
            {
                return true;
            }
            iNYG_TIPUS = Convert.ToInt32(ddlNYG_TIPUS.SelectedValue.ToString());
            iNYG_PRT_ID = Convert.ToInt32(tbNYG_PRT_ID.Text);
            iNYG_INT_ID = Convert.ToInt32(ddlNYG_INT_ID.SelectedValue.ToString());
            try
            {
                iNYG_HOZAM = Convert.ToDouble(tbNYG_HOZAM.Text);
            }
            catch
            {
                lMessage.Text = "Hozam nem megfelő formátumú!";
                tbNYG_HOZAM.Focus();
                KellMent = false;
            }
            iNYG_MEGJEGYZES = tbNYG_MEGJEGYZES.Text;
            iNYG_CREATED = DateTime.Now;
            iNYG_CREATED_BY = (int)Session["USR_ID"];
            iNYG_MODIFIED = DateTime.Now;
            iNYG_MODIFIED_BY = (int)Session["USR_ID"];


            if (KellMent)
            {
                if (!(_clNYUGDIJAK.SaveNYUGDIJAK(iNYG_ID, iNYG_TIPUS, iNYG_PRT_ID, iNYG_INT_ID, iNYG_HOZAM,
                            iNYG_MEGJEGYZES, iNYG_CREATED, iNYG_CREATED_BY, iNYG_MODIFIED, iNYG_MODIFIED_BY, ref oErr)))
                {
                    lMessage.Text = oErr;
                    return false;
                }
                else
                {
                    int si = gvNYUGDIJAK.SelectedIndex;

                    DataInit();

                    gvNYUGDIJAK.SelectedIndex = si;

                    gvNYUGDIJAK_SelectedIndexChanged(sender, e);
                    lMessage.Text = "";
                    return true;
                }
            }
            return KellMent;
        }
        protected void bVissza_Click(object sender, EventArgs e)
        {
            if (bSave_Click2(sender,e))
            Response.Redirect("wfPartnerChoice.aspx");
        }

        protected void bAlapadatok_Click(object sender, EventArgs e)
        {
            if (bSave_Click2(sender,e))
            Response.Redirect("wfPartner.aspx");
        }

        protected void bNyugdijak_Click(object sender, EventArgs e)
        {
            if (bSave_Click2(sender,e))
            Response.Redirect("wfNyugdij.aspx");
        }

        protected void bMegj_Click(object sender, EventArgs e)
        {
            if (bSave_Click2(sender,e))
            Response.Redirect("wfMegjegyzes.aspx");
        }

        protected void bHitelek_Click(object sender, EventArgs e)
        {
            if (bSave_Click2(sender,e))
            Response.Redirect("wfHitelek.aspx");
        }

        protected void bBefektetesek_Click(object sender, EventArgs e)
        {
            if (bSave_Click2(sender,e))
            Response.Redirect("wfBefektetesek.aspx");
        }

        protected void bEletBizt_Click(object sender, EventArgs e)
        {
            if (bSave_Click2(sender,e))
            Response.Redirect("wfBiztElet.aspx");
        }

        protected void bVagyonBizt_Click(object sender, EventArgs e)
        {
            if (bSave_Click2(sender,e))
            Response.Redirect("wfBiztVagyon.aspx");
        }

        protected void gvNYUGDIJAK_RowClick(object sender, RowClickEventArgs e)
        {
            if (bSave_Click2(sender,e))
            gvNYUGDIJAK_SelectedIndexChanged(sender, e);
        }

        protected void gvNYUGDIJAK_RowDataBound(object sender, GridViewRowEventArgs e)
        {
            if (e.Row.RowType == DataControlRowType.DataRow)
            {
                e.Row.ID = e.Row.Cells[0].Text;
                e.Row.Attributes["onclick"] = ClientScript.GetPostBackClientHyperlink(this.gvNYUGDIJAK, "Select$" + e.Row.RowIndex);
                //e.Row.Attributes.Add("onclick", "GridView_selectRow(this,'" + e.Row.Cells[0].Text + "')");
                e.Row.Attributes.Add("onmouseover", "GridView_mouseHover(this)");
            }
        }

        protected void bVisszaFooldalra_Click(object sender, EventArgs e)
        {
            Response.Redirect("wfDefault.aspx");
        }
    }
}
